package com.mayisoftware.mayioa.common.impl.config;


import com.mayisoftware.mayioa.common.impl.utils.StringUtils;
import com.mayisoftware.mayioa.common.impl.utils.YamlUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;

/**
 * 全局配置类
 *
 */
public class Global
{
    private static final Logger log = LoggerFactory.getLogger(Global.class);

    private static String NAME = "application.yml";

    /**
     * 当前对象实例
     */
    private static Global global = null;

    /**
     * 保存全局属性值
     */
    private static Map<String, String> map = new HashMap<String, String>();

    private Global()
    {
    }

    /**
     * 静态工厂方法 获取当前对象实例 多线程安全单例模式(使用双重同步锁)
     */

    public static synchronized Global getInstance()
    {
        if (global == null)
        {
            synchronized (Global.class)
            {
                if (global == null)
                    global = new Global();
            }
        }
        return global;
    }

    /**
     * 获取配置
     */
    public static String getConfig(String key)
    {
        String value = map.get(key);
        if (value == null)
        {
            Map<?, ?> yamlMap = null;
            try
            {
                yamlMap = YamlUtil.loadYaml(NAME);
                value = String.valueOf(YamlUtil.getProperty(yamlMap, key));
                map.put(key, value != null ? value : StringUtils.EMPTY);
            }
            catch (FileNotFoundException e)
            {
                log.error("获取全局配置异常 {}", key);
            }
        }
        return value;
    }

    /**
     * 获取项目名称
     */
    public static String getName()
    {
        return StringUtils.nvl(getConfig("mayioa.name"), "mayioa");
    }

    /**
     * 获取项目版本
     */
    public static String getVersion()
    {
        return StringUtils.nvl(getConfig("mayioa.version"), "1.0.0");
    }

    /**
     * 获取版权年份
     */
    public static String getCopyrightYear()
    {
        return StringUtils.nvl(getConfig("mayioa.copyrightYear"), "2018");
    }

    /**
     * 获取ip地址开关
     */
    public static Boolean isAddressEnabled()
    {
        return Boolean.valueOf(getConfig("mayioa.addressEnabled"));
    }

    /**
     * 获取文件上传路径
     */
    public static String getProfile()
    {
        return getConfig("mayioa.profile");
    }

    /**
     * 获取头像上传路径
     */
    public static String getAvatarPath()
    {
        return getConfig("mayioa.profile") + "avatar/";
    }


    /**
     * 获取申请文件上传路径
     */
    public static String getApplyPath()
    {
        return getConfig("mayioa.profile") + "apply/";
    }

    /**
     * 获取计划流程上传路径
     */
    public static String getPlanFlowPath()
    {
        return getConfig("mayioa.profile") + "planflow/";
    }

    /**
     * 获取计划数据上传路径
     */
    public static String getPlanDataPath()
    {
        return getConfig("mayioa.profile") + "plandata/";
    }

    /**
     * 获取人资数据上传路径
     */
    public static String getHumanDataPath()
    {
        return getConfig("mayioa.profile") + "human/";
    }

    /**
     * 获取计划数据上传路径
     */
    public static String getWorktimeDataPath()
    {
        return getConfig("mayioa.profile") + "TMWorkTime/";
    }

    public static String getPOFilePath()
    {
        return getConfig("mayioa.profile") + "POData/";
    }
    /**
     * 获取计划数据上传路径
     */
    public static String getHCDataPath()
    {
        return getConfig("mayioa.profile") + "HCData/";
    }
    /**
     * 获取计划数据上传路径
     */
    public static String getHCGoosePath()
    {
        return getConfig("mayioa.profile") + "GooseData/";
    }
    /**
     * 获取下载上传路径
     */
    public static String getDownloadPath()
    {
        return getConfig("mayioa.profile") + "download/";
    }

    /**
     * 获取计划模板路径
     */
    public static String getTemplatePath()
    {
        return getConfig("mayioa.profile") + "template/plan";
    }

    public static void createAllDefaultDirs(){
        if (!new File(getAvatarPath()).exists()){
            new File(getAvatarPath()).mkdirs();
        }

        if (!new File(getApplyPath()).exists()){
            new File(getApplyPath()).mkdirs();
        }

        if (!new File(getPlanDataPath()).exists()){
            new File(getPlanDataPath()).mkdirs();
        }

        if (!new File(getDownloadPath()).exists()){
            new File(getDownloadPath()).mkdirs();
        }
        if (!new File(getTemplatePath()).exists()){
            new File(getTemplatePath()).mkdirs();
        }

        if (!new File(getPlanFlowPath()).exists()){
            new File(getPlanFlowPath()).mkdirs();
        }
    }
}
